package com.xyht.sca_s.student_manage_system.modules.commonExamine.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.commonExamine.entity.req.SmsCommonExamineHistoryReq;
import com.xyht.sca_s.student_manage_system.modules.commonExamine.entity.req.SmsCommonExamineReq;
import com.xyht.sca_s.student_manage_system.modules.commonExamine.service.SmsCommonExamineService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 * 通用审批 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-04-24
 */
@RestController
@RequestMapping("/commonExamine")
@SecretBody
public class SmsCommonExamineController {
    @Autowired
    private SmsCommonExamineService smsCommonExamineService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty(value = "提交申请")
    @PostMapping("/add")
    public ResponseResult addCommonExamine(@RequestBody SmsCommonExamineReq smsCommonExamineReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsCommonExamineService.addCommonExamine(smsCommonExamineReq, user_id);
    }

    @ApiModelProperty(value = "撤销申请")
    @PostMapping("/cancel")
    public ResponseResult cancelCommonExamine(@RequestBody SmsCommonExamineReq smsCommonExamineReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsCommonExamineService.cancelCommonExamine(smsCommonExamineReq, user_id);
    }

    @ApiModelProperty(value = "审批申请")
    @PostMapping("/allow")
    public ResponseResult allowCommonExamine(@RequestBody SmsCommonExamineReq smsCommonExamineReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsCommonExamineService.allowCommonExamine(smsCommonExamineReq, user_id);
    }

    @ApiModelProperty(value = "审批列表")
    @GetMapping("/list")
    public ResponseResult getCommonExamineList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "search_str" , required = false) String search_str,
                                                     @RequestParam(value = "examine_type") Integer examine_type,
                                                     HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsCommonExamineService.getCommonExamineList(search_str,user_id,examine_type,pageNum,pageSize);
    }

    @ApiModelProperty(value = "审批详情")
    @GetMapping("/detail")
    public ResponseResult studentHistoryGetLeaveList(@RequestParam(value = "id") String id,
                                                     @RequestParam(value = "examine_type",required = false) Integer examine_type,
                                                     HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsCommonExamineService.getCommonExamineDetail(id,examine_type,user_id);
    }

    @ApiModelProperty(value = "表格下载")
    @GetMapping("/download")
    public ResponseResult tableDownload(@RequestParam("examine_id") String examine_id, HttpServletResponse response) {
       ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsCommonExamineService.tableDownload(examine_id,response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiModelProperty(value = "历史记录")
    @GetMapping("/history")
    public ResponseResult getExamineHistoryList(
            @RequestParam(value = "apply_user",required = false) String apply_user,
            @RequestParam(value = "examine_id",required = false) String examine_id,
            @RequestParam(value = "examine_title",required = false) String examine_title,
            @RequestParam(value = "examine_status",required = false) String examine_status,
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
        return smsCommonExamineService.getExamineHistoryList(apply_user,examine_id,examine_title,examine_status,pageNum,pageSize);
    }

    @ApiModelProperty(value = "导出历史记录")
    @PostMapping("/exportHistory")
    public ResponseResult exportExamineHistoryList(@RequestBody SmsCommonExamineHistoryReq req, HttpServletResponse response) {
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsCommonExamineService.exportExamineHistoryList(req, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }


}

